<?php

namespace app\item\controller;

use app\common\model\Configs;
use app\common\model\Videos;
use app\common\model\Pinglun;
use app\common\model\Last;
use app\common\model\Domains;
use think\Request;
use think\Db;
use think\Route;

class Video extends Base
{

    /* 首页 跳转 */
    public function index($alias='')
    {
        if ( !$alias ) return '无参数';
        $currentUrl = request()->url();
        $urlArr = explode('/',$currentUrl);
        if(isset($urlArr[3])&&ctype_digit($urlArr[3])){
            $adminId = $urlArr[3];
            $info = Videos::where(['alias'=>$alias,'admin'=>$adminId])->find();
        }else{
            $info = Videos::where(['alias'=>$alias,'sync_video_id'=>0])->find();
            $adminId = $info['admin'];
        }
        $url = fission('https') . '://' . Domains::getOneData(3,true,$info['admin']) . url('item/video/show', ['alias'=>$alias,'uid'=>$adminId, getRandStr(8,15)=>getRandStr(8,15)]);
        $this->assign('url', $url);
        return view();
    }


    /* 页面展示 */
    public function show($alias='',$uid=0)
    {
        // die;
        if ( !$alias ) return '无参数';
        // if ( !isset($_SERVER['HTTP_REFERER']) ){
        //     $url = url('item/video/index', ['alias'=>$alias]);
        // echo "维护中...";die;
        //     $this->redirect($url);
        // }

        /* 获取数据 */
        if($uid){
            $data = Videos::where(['alias'=>$alias,'admin'=>$uid])->find();
        }else{
            $data = Videos::where(['alias'=>$alias,'sync_video_id'=>0])->find();
        }

        if ( !$data ) return '没有找到数据';

        if($data['tourl']){
            $this->redirect($data['tourl']);
        }
        /* 访问量 +1 */
        $data->setInc('view');

        /* 加载淘口令 */
        $data['taokouling'] = fission('taokouling');

        $url =  url('item/video/index', ['alias'=>$alias,''=>$uid.'/']);
        $shareUrl =  str_replace('%2F','/',str_replace('//','/',$url));
	    $data['share_link']  = fission('https') . '://' . Domains::getOneData(1,true,$data['admin']) .$shareUrl;

	    if ( $data['share_img'][0] == '/' ) {
		    $data['share_img'] = fission('https') . '://' . $_SERVER['HTTP_HOST'] . $data['share_img'];
	    }


	    /* 获取全部配置 */
	    $shareinfo = Configs::field('name,value')->where('group', 'video')->select();

	    /* 数据整理成一维数组 */
	    $conf = [];
	    foreach ($shareinfo as $k=>$v){
		    $conf[$v['name']] = trim($v['value']);
	    }
	    if($data->imgs){
	        $data->imgslist = explode(',',$data->imgs);
	    }
        if($data->pinglun){
            $pinglun = explode(',',$data->pinglun);
            foreach ($pinglun as $key => $value){
                $pinglun[$key] = Pinglun::where(['id'=>$value])->find();
            }
	        $data->pinglun = $pinglun;
	    }
	    if($data["expiretime"] < time() && $data["expiretime"] != 0) {
	       // header('Location: '.$data["new_shre_url"]);
            if($data["new_shre_url"]){
                $this->redirect($data["new_shre_url"]);
                exit;
            }else{
                return '页面已过期，请填写新跳转链接';
            }
	    }
        
	    //加载到模板
	    $this->assign(['data'=>$data, 'jsondata'=>base64_encode(json_encode($data))]);
	    $this->assign(['conf'=>$conf, 'jsonconf'=>base64_encode(json_encode($conf))]);
        return view();
    }


    /* 页面展示 */
    public function last($alias='')
    {
        if ( !$alias ) return '无参数';
        // if ( !isset($_SERVER['HTTP_REFERER']) ){
        //     $url = url('item/video/index', ['alias'=>$alias]);
        //     $this->redirect($url);
        // }

        /* 获取数据 */
        $data = Last::where(['alias'=>$alias])->find();
        if ( !$data ) return '没有找到数据';

	    //加载到模板
	    $this->assign(['data'=>$data, 'jsondata'=>base64_encode(json_encode($data))]);

        return view();
    }
    /* 页面展示 */
    public function test($alias='')
    {
        if ( !$alias ) return '无参数';
        // if ( !isset($_SERVER['HTTP_REFERER']) ){
        //     $url = url('item/video/index', ['alias'=>$alias]);
        // echo "维护中...";die;
        //     $this->redirect($url);
        // }

        /* 获取数据 */
        $data = Videos::where(['alias'=>$alias])->find();
        if ( !$data ) return '没有找到数据';

        if($data['tourl']){
            $this->redirect($data['tourl']);
        }
        /* 访问量 +1 */
        $data->setInc('view');

        /* 加载淘口令 */
        $data['taokouling'] = fission('taokouling');

	    $data['share_link']  = fission('https') . '://' . Domains::getOneData(1,true,$data['admin']) . url('item/video/index', ['alias'=>$alias]);

	    if ( $data['share_img'][0] == '/' ) {
		    $data['share_img'] = fission('https') . '://' . $_SERVER['HTTP_HOST'] . $data['share_img'];
	    }


	    /* 获取全部配置 */
	    $shareinfo = Configs::field('name,value')->where('group', 'video')->select();

	    /* 数据整理成一维数组 */
	    $conf = [];
	    foreach ($shareinfo as $k=>$v){
		    $conf[$v['name']] = trim($v['value']);
	    }
	    if($data->imgs){
	        $data->imgslist = explode(',',$data->imgs);
	    }
        if($data->pinglun){
            $pinglun = explode(',',$data->pinglun);
            foreach ($pinglun as $key => $value){
                $pinglun[$key] = Pinglun::where(['id'=>$value])->find();
            }
	        $data->pinglun = $pinglun;
	    }

	    //加载到模板
	    $this->assign(['data'=>$data, 'jsondata'=>base64_encode(json_encode($data))]);
	    $this->assign(['conf'=>$conf, 'jsonconf'=>base64_encode(json_encode($conf))]);
        return view();
    }

    /* 页面展示 */
    public function show_back($alias='')
    {
        if ( !$alias ) return '无参数';

        if ( !isset($_SERVER['HTTP_REFERER']) ){
            $url = url('item/video/index', ['alias'=>$alias]);
            $this->redirect($url);
        }

        /* 获取数据 */
        $data = Videos::where(['alias'=>$alias])->find();
        if ( !$data ) return '没有找到数据';

        /* 访问量 +1 */
        $data->setInc('view');

        /* 加载淘口令 */
        $data['taokouling'] = fission('taokouling');


	    /* 是否 从指定时间开始播放 */
	    if ( $this->request->param('play', '') ){
		    $data['play'] = 1;
	    } else {
		    $data['play'] = 0;
	    }

	    /* 下一步链接 */
        $data['next_url'] = fission('https') . '://' . Domains::getOneData(3,true,$data['admin']) . url('item/video/share', ['alias'=>$alias, getRandStr(8,15)=>getRandStr(8,15)]);

        //加载到模板
        $this->assign(['data'=>$data, 'jsondata'=>base64_encode(json_encode($data))]);

        return view();
    }




	/* 分享 */
	public function share($alias='')
	{
		if ( !$alias ) return '无参数';

		if ( !isset($_SERVER['HTTP_REFERER']) ){

			$url = url('item/video/index', ['alias'=>$alias]);
			$this->redirect($url);
		}

		$data = Videos::where(['alias'=>$alias])->find();
		if ( !$data ){
			return '没有找到数据';
		}

		/* 加载淘口令 */
		$data['taokouling'] = fission('taokouling');

		$data['share_link']  = fission('https') . '://' . $_SERVER['HTTP_HOST'] . url('item/video/index', ['alias'=>$alias]);

		if ( $data['share_img'][0] == '/' ) {
			$data['share_img'] = fission('https') . '://' . $_SERVER['HTTP_HOST'] . $data['share_img'];
		}

		/* 下一步链接 */
		$data['next_url'] = $_SERVER['HTTP_REFERER'] . url('item/video/show', ['alias'=>$alias, getRandStr(8,15)=>getRandStr(8,15), 'play'=>1]);

		/* 获取全部配置 */
		$shareinfo = Configs::field('name,value')->where('group', 'video')->select();

		/* 数据整理成一维数组 */
		$conf = [];
		foreach ($shareinfo as $k=>$v){
			$conf[$v['name']] = trim($v['value']);
		}
		$this->assign(['data'=>$data, 'jsondata'=>base64_encode(json_encode($data))]);
		$this->assign(['conf'=>$conf, 'jsonconf'=>base64_encode(json_encode($conf))]);

		return view();
	}



}

